perf: BTreeMap::range() and incremental memory tracking in reassembly#24
Merged
perf: BTreeMap::range() and incremental memory tracking in reassembly#24
Conversation
Design for issue #11 — BTreeMap::range() overlap detection and incremental memory tracking in the TCP reassembly engine.
4-task plan for issue #11: buffered_bytes tracking, range-based overlap detection, and incremental total_memory tracking.
- Use saturating_sub for insert delta to prevent usize underflow - Add debug_assert! at both defensive insert guard sites - Replace unwrap_or(0) with expect() at all 5 flow removal sites - Add FIN-close total_memory test with buffered data - Add buffered_bytes assertion to retransmission dedup test - Add comments explaining pre-flush capture pattern
There was a problem hiding this comment.
Pull request overview
Optimizes the TCP reassembly engine’s hot paths by avoiding repeated O(n) scans/summations and replacing them with incremental counters and bounded BTreeMap::range() overlap checks, with new tests documenting/guarding the intended behavior.
Changes:
- Use
BTreeMap::range(..new_end)to restrict overlap scans to only potentially overlapping segments. - Add
FlowDirection::buffered_bytesand maintain it incrementally on insert/flush; use it for O(1)memory_used(). - Remove global
update_memory()recomputation and maintainTcpReassembler.total_memoryvia deltas, exposingtotal_memory()for tests.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
src/reassembly/segment.rs |
Switch overlap scan to range(..new_end); update buffered_bytes on segment inserts and flush removals. |
src/reassembly/flow.rs |
Add buffered_bytes field and make memory_used() return it with a debug-time drift check. |
src/reassembly/mod.rs |
Replace update_memory() with incremental total_memory tracking at insert/flush/removal sites; add total_memory() accessor. |
tests/reassembly_segment_tests.rs |
Add assertions/tests for buffered_bytes behavior and range boundary overlap behavior. |
tests/reassembly_flow_tests.rs |
Assert buffered_bytes initializes to 0. |
tests/reassembly_engine_tests.rs |
Add/extend tests validating total_memory() is correct across RST/timeout/finalize/FIN-close scenarios. |
docs/superpowers/specs/2026-04-06-reassembly-perf-design.md |
New design write-up for the perf changes (contains a couple of now-outdated statements). |
docs/superpowers/plans/2026-04-06-reassembly-perf.md |
New implementation plan (contains a now-outdated note about total_memory visibility). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
BTreeMap::range(..new_end)for O(log n + k) scanbuffered_bytes: usizetoFlowDirectionfor O(1) buffer size queriesupdate_memory()O(m×n) recomputation, replace with O(1) delta tracking at all 5 mutation sitestotal_memory()public accessor for testabilityCloses #11
Test plan
buffered_bytesinsert/flush/overlap/dedup,range()boundary,total_memorytracking (insert/flush/finalize/RST/FIN-close)debug_assert_eq!inmemory_used()verifies counter integrity on every call in test buildsdebug_assert!guards on defensive insert pathssaturating_subprevents usize underflow in release mode